//
//  SudokuMovieLIstViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "SudokuMovieLIstViewController.h"
#import "SudoKuListCell.h"
#import "MovieListCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "DetailViewController.h"
#import "MBProgressHUD.h"

@interface SudokuMovieLIstViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic ,retain)NSMutableArray *movieArray;
@property (nonatomic, retain)UICollectionView *collectV;
@property (nonatomic ,retain)MBProgressHUD *hub;
@end

@implementation SudokuMovieLIstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowL = [[[UICollectionViewFlowLayout alloc]init]autorelease];
    flowL.minimumLineSpacing = 20;
    flowL.itemSize = CGSizeMake(95, 160);
    flowL.sectionInset = UIEdgeInsetsMake(25, 20, 25, 20);
    
    self.collectV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 60) collectionViewLayout:flowL];
    
    self.collectV.dataSource = self;
    self.collectV .delegate = self;
    self.collectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectV];
    [_collectV  release];
    
    
    [self.collectV  registerClass:[SudoKuListCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.movieArray = [NSMutableArray array];
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
#pragma mark 让菊花旋转起来
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        //        NSLog(@"%@",result);
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"result"];
            for (NSMutableDictionary *dic in array) {
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                [movie release];
                
                
            }
            //刷新页面一定是在右数据之后刷新**************
            [self.collectV  reloadData];
            [self.hub hide:YES];
        
        
    }];
    

    
}
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
// item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

//自定义cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndntitier = @"cell";
    SudoKuListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndntitier forIndexPath:indexPath];
    Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.myMovie = mov;

    
    return cell;
    
    
}

//点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *deVC = [[DetailViewController alloc] init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    deVC.movieID = movie.movieId;
    
    [self.navigationController pushViewController:deVC animated: YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
